package com.sysfrog.cucumber;

/*
* Copyright (C) 2010 Thomas Heggelund
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

import com.sysfrog.cucumber.lexer.CucumberLexer;
import com.sysfrog.cucumber.psi.CucumberFile;
import com.sysfrog.cucumber.psi.CucumberParser;
import com.sysfrog.cucumber.psi.psiElements.CucumberStep;
import com.sysfrog.cucumber.psi.psiElements.CucumberTag;
import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.lang.ParserDefinition;
import com.intellij.lang.PsiParser;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;

import static com.sysfrog.cucumber.lexer.CucumberTokenTypes.*;
import static com.sysfrog.cucumber.psi.CucumberElementTypes.FILE;

public class CucumberParserDefinition implements ParserDefinition {
    @NotNull
    public Lexer createLexer(Project project) {
        return new CucumberLexer();
    }

    public PsiParser createParser(Project project) {
        return new CucumberParser();
    }

    public IFileElementType getFileNodeType() {
        return FILE;
    }

    @NotNull
    public TokenSet getWhitespaceTokens() {
        return TokenSet.create(WHITESPACE, EOL, EOF);
    }

    @NotNull
    public TokenSet getCommentTokens() {
        return COMMENTS;
    }

    @NotNull
    public TokenSet getStringLiteralElements() {
        return TokenSet.create(STRING_LITERAL); 
    }

    public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
        return SpaceRequirements.MUST;
    }

    public PsiFile createFile(FileViewProvider viewProvider) {
        return new CucumberFile(viewProvider);
    }

    @NotNull
    public PsiElement createElement(ASTNode node) {
        final IElementType type = node.getElementType();

        if (type == com.sysfrog.cucumber.lexer.CucumberTokenTypesNew.STEP_MEAT) {
            return new CucumberStep(node);
        } else if (type == com.sysfrog.cucumber.lexer.CucumberTokenTypesNew.TAG) {
            return new CucumberTag(node);
        } 

        // return new DefaultType(node);
        return new ASTWrapperPsiElement(node);

    }
}
